#include "datatemp.h"
#include <QSettings>
#include <QDebug>

#define CONFFILENAME "config.ini"
#define DEFGROUPNAME "setting"
#define DEVICE_ID       "device_id"
#define DEVICE_KEY      "device_key"
#define REQ_TIME        "req_time"
#define UPLOAD_TIME     "upload_time"
#define RUN_MODE        "run_mode"
#define SERIAL_NAME     "serial_name"
#define SERIAL_BAUE     "serial_baudrate"
#define NET_IP          "net_ip"
#define NET_PORT        "net_port"
DataTemp *DataTemp::obj= nullptr;
DataTemp::DataTemp(QObject *parent) : QObject(parent)
{
initData();
}

DataTemp *DataTemp::getObject()
{
if(obj == nullptr)
    obj = new DataTemp;
return obj;
}

void DataTemp::saveData()
{
    QSettings set(CONFFILENAME, QSettings::IniFormat);
    set.beginGroup(DEFGROUPNAME);
    set.setValue(DEVICE_ID, deviceId);
    set.setValue(DEVICE_KEY, deviceKey);
    set.setValue(REQ_TIME, reqTime);
    set.setValue(UPLOAD_TIME, uploadTime);
    set.setValue(RUN_MODE, mode);
    set.setValue(SERIAL_NAME, serialName);
    set.setValue(SERIAL_BAUE, serialBaudRate);
    set.setValue(NET_IP, netIp);
    set.setValue(NET_PORT, netPort);
    set.endGroup();

}

void DataTemp::initData()
{
    QSettings set(CONFFILENAME, QSettings::IniFormat);
    set.beginGroup(DEFGROUPNAME);
    QStringList keys = set.allKeys();
    if(!keys.contains(DEVICE_ID))
        set.setValue(DEVICE_ID, "D0001");
    deviceId = set.value(DEVICE_ID).toString();
    if(!keys.contains(DEVICE_KEY))
        set.setValue(DEVICE_KEY, "123456");
    deviceKey = set.value(DEVICE_KEY).toString();
    if(!keys.contains(REQ_TIME))
        set.setValue(REQ_TIME, 17);
    reqTime = set.value(REQ_TIME).toInt();
    if(!keys.contains(UPLOAD_TIME))
        set.setValue(UPLOAD_TIME, 17);
    uploadTime = set.value(UPLOAD_TIME).toInt();
    if(!keys.contains(RUN_MODE))
        set.setValue(RUN_MODE, 0);
    mode = set.value(RUN_MODE).toInt();
    if(!keys.contains(SERIAL_NAME))
        set.setValue(SERIAL_NAME, "COM4");
    serialName = set.value(SERIAL_NAME).toString();
    if(!keys.contains(SERIAL_BAUE))
        set.setValue(SERIAL_BAUE,115200);
    serialBaudRate = set.value(SERIAL_BAUE).toInt();
    if(!keys.contains(NET_IP))
        set.setValue(NET_IP, "192.168.43.52");
    netIp = set.value(NET_IP).toString();
    if(!keys.contains(NET_PORT))
        set.setValue(NET_PORT, 10086);
    netPort = set.value(NET_PORT).toInt();
    set.endGroup();
}



